<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-alert
        title="注意：只允许第三级分类设置相关参数！"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 选择商品 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类：</span>
          <el-cascader
            v-model="cateKeys"
            :options="catelist"
            :props="cateProps"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>
      <!-- 页签 -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" size="mini" :disabled="isBtn"
            >添加参数</el-button
          >
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" size="mini" :disabled="isBtn"
            >添加属性</el-button
          >
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getGoodsCategories } from "@/api/cate";
import { getGoodsAttribute } from "@/api/params";
export default {
  name: "ParaMs",
  data() {
    return {
      catelist: [],
      cateProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
      },
      cateKeys: [],
      activeName: "many",
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const res = await getGoodsCategories(this.goodsId, {
        sel: this.activeName,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败");
      }
      this.catelist = res.data;
      // console.log(this.catelist);
    },
    handleChange() {
      this.getParams();
    },
    async getParams() {
      // console.log(this.cateKeys);
      if (this.cateKeys.length !== 3) {
        this.cateKeys = [];
        return;
      }
      // console.log(this.cateKeys);
      const res = await getGoodsAttribute({
        params: { sel: this.activeName },
      });
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取参数列表失败");
      }
      console.log(res.data);
    },
    handleTabClick() {
      // console.log(this.activeName);
      this.getParams();
    },
  },
  computed: {
    isBtn() {
      if (this.cateKeys.length !== 3) {
        return true;
      }
      return false;
    },
    cateId() {
      if (this.cateKeys.length === 3) {
        return this.cateKeys[2];
      }
      return null;
    },
  },
};
</script>

<style lang="scss" scoped>
.cat_opt {
  margin: 15px 0;
}
</style>
